<?php
header("Content-Type: text/html;charset=utf-8");
ini_set('memory_limit','-1');
// php最大执行时间设置为：半个小时，php原来默认为30秒，爬不完
ini_set('max_execution_time', '0');
include '../vendor/autoload.php';// 使用composer安装后引入目录
include_once 'class/Mysql.php';
use QL\QueryList;

try {
    $mysql = Mysql::getMysql();
    $obj = $mysql->query('select books.book_id,book_source.source_url  from books inner join book_source on books.book_id = book_source.book_id  where book_source.status = 1 and books.status=1  LIMIT 1');
    $books = $obj->fetchAll();
    foreach ($books as $v){
        $result =pickBook($v['book_id'],$v['source_url']);
        if($result){
           $mysql->query('update source set status = 1  where id= '.$v['id']);
           $mysql->query('update book_source set status = 2  where book_id= '.$v['book_id']);
        }
    }


}catch (Exception $e) {

    $exceptionlog = '文件 ['.$e->getFile().'] 第 '.$e->getLine().' 行出现未处理的异常'.PHP_EOL;
    $exceptionlog .= '代码:'.$e->getCode().PHP_EOL;
    $exceptionlog .= '信息:'.$e->getMessage().PHP_EOL;
    $exceptionlog .= '跟踪信息：'.$e->getTraceAsString();
    //记录错误
    error_log(date('Y-m-d H:i:s') . print_r($exceptionlog, 1) . PHP_EOL, 3, 'logs/'.date('Y-m-d').'_'.'exception.log');
}

function pickBook($book_id,$book_url){
    try{
    $book_url = str_ireplace("info","wapbook",$book_url);
    $book_url_first = $book_url.'_1/';
    $html = iconv('GBK', 'UTF-8', file_get_contents($book_url_first));
    $max = QueryList::html($html)->find('.showpage li a')->attrs('href');
    $max_menus = $max->all();
    $max_menus = array_unique(array_filter($max_menus));
    array_unshift($max_menus,str_ireplace('https://m.gzbpi.com','',$book_url_first));
    foreach ($max_menus as $p_url){
        $p_url = 'https://m.gzbpi.com' .$p_url;
        $html = iconv('GBK', 'UTF-8', file_get_contents($p_url));
        $menus = QueryList::html($html)->find('.showpage li a')->attrs('href');
        $menus = array_unique(array_filter($menus->all()));
        array_unshift($menus,str_ireplace('https://m.gzbpi.com','',$p_url));
        foreach ($menus as $menu_url) {
            $menu_url ='https://m.gzbpi.com' .$menu_url;
            $html = iconv('GBK', 'UTF-8', file_get_contents($menu_url));
            $ql = QueryList::html($html);
            $pages = $ql->find('.lb li a')->remove('.lb .last')->map(function ($item) {
                $rt = [];
                $rt['href'] = $item->attr('href');
                $rt['title'] = $item->text();
                return $rt;
            });
            $pages = $pages->all();
            foreach ($pages as $k => $page) {
                if ($k == 0 || $k == count($pages) - 1) continue;
                $page_url = 'https://m.gzbpi.com' . $page['href'];
                $page_url = rtrim($page_url, '.html');
                for ($i = 1; $i < 10; $i++) {
                    if ($i == 1) {
                        $new_page_url = $page_url . '.html';
                    } else {
                        $new_page_url = $page_url . '-' . $i . '.html';
                    }
                    $ql = QueryList::html(file_get_contents($new_page_url));
                    $pageNum = pathinfo($page['href'])["filename"];
                    $title = $ql->find('.c_title')->text();
                    $content = $ql->find('#content-txt')->text();
                    $content = str_ireplace("一秒记住域名:", '', $content);
                    $content = str_ireplace("gzbpi.com", '', $content);
                    $content = str_ireplace("瓜子*小说网", '', $content);
                    $content = str_ireplace("www.gzbpi.com", '', $content);
                    $content = str_ireplace("\" www.gzbpi.com", '', $content);
                    $content = str_ireplace("瓜*子*小*说*网了", '', $content);
                    $content = str_ireplace("\" www.", '', $content);
                    $content = str_ireplace("  \"瓜*子*小*说*网", '', $content);
                    $content = str_ireplace("阅读提示：", "", $content);
                    $content = str_ireplace("系统检查到无法加载当前章节的下一页内容，请单击屏幕中间，点击右下角或者右上角找到“关闭畅读”按纽即可阅读完整小说内容。", '', $content);
                    error_log($book_id.','.$title.','.$content . PHP_EOL, 3, 'data/pages.csv');
                }
            }
        }
    }

    return true;

    }catch (Exception $e) {
        $exceptionlog = '文件 ['.$e->getFile().'] 第 '.$e->getLine().' 行出现未处理的异常'.PHP_EOL;
        $exceptionlog .= '代码:'.$e->getCode().PHP_EOL;
        $exceptionlog .= '信息:'.$e->getMessage().PHP_EOL;
        $exceptionlog .= '跟踪信息：'.$e->getTraceAsString();
        //记录错误
        error_log(date('Y-m-d H:i:s') . print_r($exceptionlog, 1) . PHP_EOL, 3, 'logs/'.date('Y-m-d').'_'.'pick_exception.log');
    }

}


function cn2num($string)
{
    if(is_numeric($string)){
        return $string;
    }
    // '仟' => '千','佰' => '百','拾' => '十',
    $string = str_replace('仟', '千', $string);
    $string = str_replace('佰', '百', $string);
    $string = str_replace('拾', '十', $string);
    $num = 0;
    $wan = explode('万', $string);
    if (count($wan) > 1) {
        $num += cn2num($wan[0]) * 10000;
        $string = $wan[1];
    }
    $qian = explode('千', $string);
    if (count($qian) > 1) {
        $num += cn2num($qian[0]) * 1000;
        $string = $qian[1];
    }
    $bai = explode('百', $string);
    if (count($bai) > 1) {
        $num += cn2num($bai[0]) * 100;
        $string = $bai[1];
    }
    $shi = explode('十', $string);
    if (count($shi) > 1) {
        $num += cn2num($shi[0] ? $shi[0] : '一') * 10;
        $string = $shi[1] ? $shi[1] : '零';
    }
    $ling = explode('零', $string);
    if (count($ling) > 1) {
        $string = $ling[1];
    }
    $d = array(
        '一' => '1','二' => '2','三' => '3','四' => '4','五' => '5','六' => '6','七' => '7','八' => '8','九' => '9',
        '壹' => '1','贰' => '2','叁' => '3','肆' => '4','伍' => '5','陆' => '6','柒' => '7','捌' => '8','玖' => '9',
        '零' => 0, '0' => 0, 'O' => 0, 'o' => 0,
        '两' => 2
    );
    return $num + @$d[$string];
}




?>